//
//  WaitPublishView.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/1.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "WaitPublishView.h"
#import "MZTimerLabel.h"
#import "RolesVC.h"
#import "PdcDetailVC.h"
#import "WYPublicWeb.h"
//计算详细
#define JSXX_URL @"api/ApiGoods/getGameRulesDetail?id="
//获取最新一期的期数
#define NEW_QISHU_URL @"/api/ApiGoods/getLatestGoodsIdById"
//商品详情
#define DETAIL_URL @"/api/ApiGoods/getGoodsDetail"
//新的最新揭晓界面的接口
#define  DETAIL_URL_NEW @"/api/ApiGoods/getGoodsDetailQuser"

@interface WaitPublishView ()
{
    __weak IBOutlet UILabel *jxzLab;
    __weak IBOutlet UILabel *titleLab;
    __weak IBOutlet UIView *bgView;
    __weak IBOutlet UIButton *jsBtn;
    __weak IBOutlet UILabel *lab;
    __weak IBOutlet UILabel *timeLab;
    
    __weak IBOutlet UILabel *jxLab;
    __weak IBOutlet UILabel *tsLab;
    
    //倒计时
    MZTimerLabel *timerExample1;
}

@property (nonatomic , assign) NSInteger freshCount;
@end
@implementation WaitPublishView

- (void)awakeFromNib
{
    //防止循环刷新
    self.freshCount = 0;
    
    if(WYMWITH > 320){
        tsLab.font = [UIFont systemFontOfSize:14];
    }
    jxzLab.textColor = QBB_RED_COLOR;
    jxzLab.layer.borderWidth = 0.5;
    jxzLab.layer.borderColor = QBB_RED_COLOR.CGColor;
    jxzLab.layer.masksToBounds = YES;
    jxzLab.layer.cornerRadius = 2;
    bgView.layer.cornerRadius = 2;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = QBB_RED_COLOR;
    jsBtn.layer.cornerRadius = 2;
    jsBtn.layer.masksToBounds = YES;
    jsBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    jsBtn.layer.borderWidth = 1;
}
//填充view数据的方法
- (void)fillViewWithDic:(NSDictionary *)dic
{
    titleLab.text = [NSString stringWithFormat:@"(第%@期)%@",dic[@"goods"][@"qishu"],dic[@"goods"][@"title"]];
    _pdc_id = dic[@"goods"][@"id"];
    NSNumber *t_n = dic[@"goods"][@"countdown"];
    if ([t_n integerValue] >= 0) {
        //倒计时处理
        timerExample1 = [[MZTimerLabel alloc] initWithLabel:timeLab andTimerType:MZTimerLabelTypeTimer];
        [timerExample1 setCountDownTime:[t_n integerValue]+1];
        timerExample1.resetTimerAfterFinish = YES;
        timerExample1.timeFormat = @"mm:ss:SS";
        
        if(![timerExample1 counting]){
            
            [timerExample1 startWithEndingBlock:^(NSTimeInterval countTime) {
                timeLab.hidden = YES;
                jxLab.text = @"正在开奖";
                
                [self requestForDetail];
            }];
        }

    }else{
        timeLab.hidden = YES;
        jxLab.text = @"正在开奖";

    }
}
//获取控制器
- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}
//获取商品详情接口
- (void)requestForDetail
{
    if (self.pdc_id != nil && self.pdc_id.length != 0) {
        
        
        //新接口
//        NSDictionary *params = @{@"id":self.pdc_id,@"get_go_total":@1};
//        [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,DETAIL_URL_NEW] Parameters:params successBlock:^(NSDictionary *dic) {
//            
//            
//            PdcDetailVC *detailVC = (PdcDetailVC*)[self viewController];
//            detailVC.pdc_id = self.pdc_id;
//            if ([[WYPublic GetCurrentviewController]class] == [PdcDetailVC class]) {
//                [self removeFromSuperview];
//                [detailVC reloadTabHeaderWithDic:dic];
//            }
//            
//        } failedBlock:^{
//            
//            NSLog(DETAIL_URL_NEW);
//            NSLog(NSSTRING_PRETTY_FUNCTION);
//            
//        } needLog:NO];
//        

        
        NSDictionary *params = @{@"id":self.pdc_id};
        [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,DETAIL_URL] Parameters:params successBlock:^(NSDictionary *dic) {
            NSDictionary *pdc = dic[@"data"][@"goods"];
            
            NSString *pdc_open_time_flag = pdc[@"q_showtime"];
            if ([pdc_open_time_flag isEqualToString:@"Y"]) {
                if (self.freshCount < 4){
                    self.freshCount ++;
                    double delayInSeconds = 2.0f;
                    __block WaitPublishView* bself = self;
                    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
                    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                        
                        [bself requestForDetail];
                    });
                    return ;
                }
            }else{
                //可以显示开奖 并且将计数器归0
                self.freshCount = 0;
                PdcDetailVC *detailVC = (PdcDetailVC*)[self viewController];
                detailVC.pdc_id = self.pdc_id;
                if ([[WYPublic GetCurrentviewController]class] == [PdcDetailVC class]) {
                    [self removeFromSuperview];
                    [detailVC reloadTabHeaderWithDic:dic];
                }
        
            }
            
        } failedBlock:^{
            
        } needLog:NO];
        
    }
}

//计算详细 进入开奖规则界面
- (IBAction)jsxxBtnOnClick:(id)sender {
    WYPublicWeb *web = [[WYPublicWeb alloc]init];
    web.mtitle = @"计算详情";
    NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
    web.urlStr = [NSString stringWithFormat:@"%@%@%@",newIp,JSXX_URL,self.pdc_id];
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];
}

@end
